package com.bsg.upm.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bsg.upm.annotation.Auth;
import com.bsg.upm.constant.UserRoleConstants;
import com.bsg.upm.exception.APIException;
import com.bsg.upm.http.RespJson;
import com.bsg.upm.http.RespJsonFactory;
import com.bsg.upm.service.UserService;

/**
 * 用户管理控制层
 * 
 * @author HCK
 *
 */
@Controller
@RequestMapping("/v1.0/users")
public class UserController {

	@Resource
	private UserService userService;

	/**
	 * 【查询】
	 * 
	 * @param paramMap
	 *            请求参数
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@Auth(value = { UserRoleConstants.ADMIN })
	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public RespJson listUser(@RequestParam Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			Map<String, Object> searchParamMap = new HashMap<String, Object>();
			respJson = userService.list(searchParamMap);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【详情】
	 * 
	 * @param username
	 *            用户名
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{username}", method = RequestMethod.GET)
	@ResponseBody
	public RespJson getUser(@PathVariable("username") String username, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = userService.get(username);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【注册】
	 * 
	 * @param paramMap
	 *            请求参数
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@Auth(value = { UserRoleConstants.ADMIN })
	@RequestMapping(value = "/register", method = RequestMethod.POST)
	@ResponseBody
	public RespJson registerUser(@RequestBody Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = userService.save(paramMap);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【UPMSSO注册】
	 * 
	 * @param paramMap
	 *            请求参数
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/sso_register", method = RequestMethod.POST)
	@ResponseBody
	public RespJson registerSSOUser(@RequestBody Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = userService.save(paramMap);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【编辑】
	 *
	 * @param username
	 *            用户名
	 * @param paramMap
	 *            请求参数
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@Auth(value = { UserRoleConstants.ADMIN })
	@RequestMapping(value = "/{username}", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson updateUser(@PathVariable("username") String username, @RequestBody Map<String, Object> paramMap,
			HttpServletResponse response) {
		RespJson respJson = null;
		try {
			paramMap.put("username", username);
			respJson = userService.update(paramMap);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【密码变更】
	 *
	 * @param username
	 *            用户名
	 * @param paramMap
	 *            请求参数
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{username}/pwd", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson updatePwd(@PathVariable("username") String username, @RequestBody Map<String, Object> paramMap,
			HttpServletResponse response) {
		RespJson respJson = null;
		try {
			paramMap.put("username", username);
			respJson = userService.updatePwd(paramMap);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【密码重置】
	 *
	 * @param username
	 *            用户名
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{username}/reset", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson resetPwd(@PathVariable("username") String username, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = userService.resetPwd(username);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【启用】
	 * 
	 * @param username
	 *            用户名
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@Auth(value = { UserRoleConstants.ADMIN })
	@RequestMapping(value = "/{username}/enable", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson enableUser(@PathVariable("username") String username, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = userService.enable(username, true);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【停用】
	 * 
	 * @param username
	 *            用户名
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@Auth(value = { UserRoleConstants.ADMIN })
	@RequestMapping(value = "/{username}/disable", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson disableUser(@PathVariable("username") String username, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = userService.enable(username, false);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}
}
